package com.wxandroid.newwidgetdemo;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.wxandroid.newwidgetdemo.adapter.MyRecycleViewAdapter;
import com.wxandroid.newwidgetdemo.adapter.MyStaggeredViewAdapter;
import com.wxandroid.newwidgetdemo.utils.SnackbarUtil;

/**
 * Created by Administrator on 2016/12/24.
 */

public class MyFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener,
        MyRecycleViewAdapter.OnItemClickListener,
        MyStaggeredViewAdapter.OnItemClickListener {

    private View mView;
    private SwipeRefreshLayout mSwipeRefreshLayout;
    private RecyclerView myRecycleView;
    private MyStaggeredViewAdapter myStaggeredViewAdapter;
    private MyRecycleViewAdapter myRecycleViewAdapter;
    private RecyclerView.LayoutManager layoutManager;

    private int flag = 0;
    private static final int VERTICAL_LIST = 0;
    private static final int HORIZONTAL_LIST = 1;
    private static final int VERTICAL_GRID = 2;
    private static final int HORIZONTAL_GRID = 3;
    private static final int STAGGERED_GRID = 4;

    private static final int SPAN_COUNT = 2;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_main, container, false);
        return mView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mSwipeRefreshLayout = (SwipeRefreshLayout) mView.findViewById(R.id.id_swiperefreshlayout);
        myRecycleView = (RecyclerView) mView.findViewById(R.id.id_recyclerview);

        flag = (int) getArguments().get("flag");
        configRecyclerView();

        //刷新时，指示器变换颜色
        mSwipeRefreshLayout.setColorSchemeResources(R.color.main_blue_light, R.color.main_blue_dark);
        mSwipeRefreshLayout.setOnRefreshListener(this);
    }

    private void configRecyclerView() {
        switch (flag) {
            case VERTICAL_LIST:
                layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
                break;
            case HORIZONTAL_LIST:
                layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
                break;
            case VERTICAL_GRID:
                layoutManager = new GridLayoutManager(getActivity(), SPAN_COUNT, GridLayoutManager.VERTICAL, false);
                break;
            case HORIZONTAL_GRID:
                layoutManager = new GridLayoutManager(getActivity(), SPAN_COUNT, LinearLayoutManager.HORIZONTAL, false);
                break;
            case STAGGERED_GRID:
                layoutManager = new StaggeredGridLayoutManager(SPAN_COUNT, StaggeredGridLayoutManager.VERTICAL);
                break;
        }
        if (flag != STAGGERED_GRID) {
            myRecycleViewAdapter = new MyRecycleViewAdapter(getActivity());
            myRecycleViewAdapter.setOnItemClickListener(this);
            myRecycleView.setAdapter(myRecycleViewAdapter);

        } else {
            myStaggeredViewAdapter = new MyStaggeredViewAdapter(getActivity());
            myStaggeredViewAdapter.setOnItemClickListener(this);
            myRecycleView.setAdapter(myStaggeredViewAdapter);
        }

        myRecycleView.setLayoutManager(layoutManager);

    }

    @Override
    public void onRefresh() {
//刷新数据
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mSwipeRefreshLayout.setRefreshing(false);
                int temp = (int) (Math.random() * 10);
                if (flag != STAGGERED_GRID) {
                    myRecycleViewAdapter.mDatas.add(0, "new" + temp);
                    myRecycleViewAdapter.notifyDataSetChanged();
                }else{
                    myStaggeredViewAdapter.mDatas.add(0,"new"+temp);
                    myStaggeredViewAdapter.mHeights.add(0,(int) (Math.random() * 300) + 200);
                    myStaggeredViewAdapter.notifyDataSetChanged();
                }
            }
        }, 2000);
    }

    @Override
    public void onItemClick(View view, int position) {
        SnackbarUtil.show(myRecycleView, getString(R.string.item_clicked), 0);
    }

    @Override
    public void onItemLongClick(View view, int position) {
        SnackbarUtil.show(myRecycleView, getString(R.string.item_longclicked), 0);

    }
}
